package com.hmall.item.web;

import com.hmall.common.dto.PageDTO;
import com.hmall.common.dto.SearchParams;
import com.hmall.common.dto.SearchVo;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;
    @GetMapping("/list")
    public PageDTO<Item> getPage(Integer page,Integer size){
        PageDTO<Item> pageDto=itemService.getPage(page,size);
        return pageDto;
    }

    @PostMapping
    public void save(@RequestBody Item item){
        itemService.insert(item);
    }
    @GetMapping("/{id}")
    public Item getById(@PathVariable Long id){
        Item item = itemService.getById(id);
        return item;
    }
    @PutMapping
    public void update(@RequestBody Item item){
     itemService.updateById(item);
    }
    @PutMapping("/status/{id}/{status}")
    public void updateStatus(@PathVariable Long id,@PathVariable Integer status ){
     itemService.updateStatus(id,status);
    }
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id){
     itemService.removeById(id);
    }

    @GetMapping("/filters")
    public SearchVo fliters(@RequestBody SearchParams searchParams){
        SearchVo searchVo=itemService.fliters(searchParams);
        return searchVo;
    }
    @GetMapping("/list1")
    public PageDTO getlist(@RequestBody SearchParams searchParams){
        PageDTO pageDTO= itemService.getList(searchParams);
        return pageDTO;
    }
    @PutMapping("/stock/{itemId}/{num}")
    public void decreamStock(@PathVariable Long itemId,@PathVariable Integer num){
        itemService.decreamStock(itemId,num);
    }
    @PutMapping("/{itemId}/{num}")
    public void recoverStock(@PathVariable Long itemId,@PathVariable Integer num){
        itemService.recoverStock(itemId,num);
    }
}
